package cayte.xiami.in.ui.setting;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import net.youmi.android.offers.PointsChangeNotify;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import cayte.xiami.in.D;
import cayte.xiami.in.R;
import cayte.xiami.in.ad.PointManager;
import cayte.xiami.in.ad.AdUtil;
import cayte.xiami.in.helper.Check;
import cayte.xiami.in.helper.DBHelper;
import cayte.xiami.in.helper.SpfHelper;
import cayte.xiami.in.ui.TitleView;

import com.umeng.analytics.MobclickAgent;

public class AutoCheckinActivity extends Activity implements PointsChangeNotify {

	private SettingItem autoSwitch;
	private TextView hasPoints, predictDate;
	private View autoToPoints;
	private TitleView title;

	private SpfHelper spfHelper;
	private PointManager pointManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.auto_checkin);

		spfHelper = new SpfHelper(this);
		pointManager = PointManager.instance(this);

		title = new TitleView(this);
		title.setText(R.string.autoCheckin);
		title.setLeftImage(R.drawable.back, new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onBackPressed();
			}
		});
		autoSwitch = new SettingItem(findViewById(R.id.autoSwitch));
		hasPoints = (TextView) findViewById(R.id.autoHasPoints);
		predictDate = (TextView) findViewById(R.id.autoPredictDate);
		((TextView) findViewById(R.id.autoRules)).setText(String.format(
				getString(R.string.autoRules), Check.CHECK_POINT));
		autoToPoints = findViewById(R.id.autoToPoints);

		autoSwitch.setCheckVisible(true);
		autoSwitch.setTitle(R.string.autoSwitch);
		autoSwitch.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (D.OWNER) {
					boolean isAuto = !spfHelper.isAutoCheck();
					spfHelper.saveAutoCheck(isAuto);
					initData();
				} else {
					DBHelper db = new DBHelper(AutoCheckinActivity.this);
					int accSize = db.query().size();
					if (pointManager.queryPoints() >= (accSize * Check.CHECK_POINT)) {
						boolean isAuto = !spfHelper.isAutoCheck();
						spfHelper.saveAutoCheck(isAuto);
						initData();
					} else {
						Toast.makeText(AutoCheckinActivity.this,
								R.string.autoPointsNotEnoughTip,
								Toast.LENGTH_LONG).show();
					}
				}
			}
		});

		autoToPoints.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
				intent.setClass(AutoCheckinActivity.this,
						GoldCenterActivity.class);
				AutoCheckinActivity.this.startActivity(intent);
				AutoCheckinActivity.this.overridePendingTransition(
						R.anim.fade_in, R.anim.fade_out);
				AutoCheckinActivity.this.finish();
			}
		});
		//
		AdUtil.register(this, this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
		initData();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@SuppressLint("SimpleDateFormat")
	private void initData() {
		// TODO Auto-generated method stub
		if (spfHelper.isAutoCheck()) {
			autoSwitch.setChecked(true);
			autoSwitch.setTip(R.string.open);
		} else {
			autoSwitch.setChecked(false);
			autoSwitch.setTip(R.string.close);
		}

		hasPoints.setText(getString(R.string.autoHasPoints)
				+ pointManager.queryPoints());

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		int days = pointManager.queryPoints() / Check.CHECK_POINT;
		DBHelper db = new DBHelper(this);
		int accSize = db.query().size();
		if (accSize > 1)
			days = days / accSize;
		calendar.add(Calendar.DATE, days);
		String date = sdf.format(calendar.getTime());
		predictDate.setText(getString(R.string.autoPredictDate) + date);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		this.finish();
		overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
	}

	@Override
	public void onPointBalanceChange(int points) {
		hasPoints.setText(getString(R.string.autoHasPoints) + points);
		MobclickAgent.onEvent(this, "points_record",
				points + "-" + Check.getAccount(this));
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		//
		AdUtil.unregister(this, this);
	}
}
